package com.ecommerce.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ecommerce.entity.*;
import org.springframework.stereotype.Component;

/**
 * 条件构造器
 *
 * @author Administrator
 */
@Component
public class WrapperUtils {

    /**
     * 商品查询条件构造器
     *
     * @param product 商品
     * @return wrapper
     */
    public LambdaQueryWrapper<Product> productWrapper(Product product) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        if (product != null) {
            if (product.getProductId() != null) {
                wrapper.eq(Product::getProductId, product.getProductId());
            }
            if (product.getProductName() != null) {
                wrapper.like(Product::getProductName, product.getProductName());
            }
            if (product.getCategoryId() != null) {
                wrapper.eq(Product::getCategoryId, product.getCategoryId());
            }
            if (product.getShopId() != null) {
                wrapper.eq(Product::getShopId, product.getShopId());
            }
        }
        return wrapper;
    }

    /**
     * 订单查询条件构造器
     *
     * @param order 订单
     * @return wrapper
     */
    public LambdaQueryWrapper<Order> orderWrapper(Order order) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        if (order != null) {
            if (order.getShopId() != null) {
                wrapper.eq(Order::getShopId, order.getShopId());
            }
            if (order.getUserId() != null) {
                wrapper.eq(Order::getUserId, order.getUserId());
            }
            if (order.getProductName() != null) {
                wrapper.like(Order::getProductName, order.getProductName());
            }
            if (order.getOrderStatus() != null) {
                wrapper.eq(Order::getOrderStatus, order.getOrderStatus());
            }
        }
        return wrapper;
    }

    /**
     * 优惠券查询条件构造器
     *
     * @param coupon 优惠券
     * @return wrapper
     */
    public LambdaQueryWrapper<Coupon> couponWrapper(Coupon coupon) {
        LambdaQueryWrapper<Coupon> wrapper = new LambdaQueryWrapper<>();
        if (coupon != null) {
            if (coupon.getCouponId() != null) {
                wrapper.eq(Coupon::getCouponId, coupon.getCouponId());
            }
            if (coupon.getCouponName() != null) {
                wrapper.like(Coupon::getCouponName, coupon.getCouponName());
            }
            if (coupon.getReceiveType() != null) {
                wrapper.eq(Coupon::getReceiveType, coupon.getReceiveType());
            }
            if (coupon.getShopId() != null) {
                wrapper.eq(Coupon::getShopId, coupon.getShopId());
            }
        }
        return wrapper;
    }

    /**
     * 优惠券领取信息查询条件构造器
     *
     * @param couponReceive 优惠券领取信息
     * @return wrapper
     */
    public LambdaQueryWrapper<CouponReceive> couponReceiveWrapper(CouponReceive couponReceive) {
        LambdaQueryWrapper<CouponReceive> wrapper = new LambdaQueryWrapper<>();
        if (couponReceive != null) {
            if (couponReceive.getCouponId() != null) {
                wrapper.eq(CouponReceive::getCouponId, couponReceive.getCouponId());
            }
            if (couponReceive.getUserId() != null) {
                wrapper.like(CouponReceive::getUserId, couponReceive.getUserId());
            }
            if (couponReceive.getStatus() != null) {
                wrapper.eq(CouponReceive::getStatus, couponReceive.getStatus());
            }
            if (couponReceive.getReceiveId() != null) {
                wrapper.eq(CouponReceive::getReceiveId, couponReceive.getReceiveId());
            }
        }
        return wrapper;
    }

    /**
     * 用户查询条件构造器
     *
     * @param user 用户
     * @return wrapper
     */
    public LambdaQueryWrapper<User> userWrapper(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if (user != null) {

            if (user.getUserId() != null) {
                wrapper.eq(User::getUserId, user.getUserId());
            }
            if (user.getAccount() != null) {
                wrapper.eq(User::getAccount, user.getAccount());
            }
        }
        return wrapper;
    }

    /**
     * 分类查询条件构造器
     *
     * @param category 分类
     * @return wrapper
     */
    public LambdaQueryWrapper<Category> categoryWrapper(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        if (category != null) {
            if (category.getCategoryId() != null) {
                wrapper.eq(Category::getCategoryId, category.getCategoryId());
            }
            if (category.getCategoryName() != null) {
                wrapper.like(Category::getCategoryName, category.getCategoryName());
            }
            if (category.getShopId() != null) {
                wrapper.eq(Category::getShopId, category.getShopId());
            }
        }
        return wrapper;
    }

    /**
     * 广告查询条件构造器
     *
     * @param advertising 广告
     * @return wrapper
     */
    public LambdaQueryWrapper<Advertising> advertisingWrapper(Advertising advertising) {
        LambdaQueryWrapper<Advertising> wrapper = new LambdaQueryWrapper<>();
        if (advertising != null) {
            if (advertising.getAdId() != null) {
                wrapper.eq(Advertising::getAdId, advertising.getAdId());
            }
            if (advertising.getAdName() != null) {
                wrapper.like(Advertising::getAdName, advertising.getAdName());
            }
            if (advertising.getAdType() != null) {
                wrapper.like(Advertising::getAdType, advertising.getAdType());
            }
            if (advertising.getStatus() != null) {
                wrapper.like(Advertising::getStatus, advertising.getStatus());
            }
        }
        return wrapper;
    }

}
